import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('81.71.137.167'))
channel = connection.channel()

# exchange='m1', exchange(秘书的名称)
# exchange_type='fanout', 秘书以此工作方式将消息送给所有队列
channel.exchange_declare(exchange='m1', exchange_type='fanout')

# 随机生成一个队列
result = channel.queue_declare('', exclusive=True)
queue_name = result.method.queue

# 让exchange和queue进行绑定
channel.queue_bind(queue_name, 'm1')

def callback(ch, method, properties, body):
    print(body)
    # 如果下面auto_ack=False,就代表着接收到body后要手动去回复服务端, 如果为True的话下面这行就不需要写了
    ch.basic_ack(delivery_tag=method.delivery_tag)

# 加了下面这行代表消费者不再轮流消费了, 而是改成谁闲置谁来消费
channel.basic_qos(prefetch_count=1)

channel.basic_consume(queue_name, callback, auto_ack=False)

channel.start_consuming()